package com.xpec.c4.game.utility;

import java.util.Random;

import com.xpec.c4.service.group.datalayer.IShareGroupData;

public class GUID implements Comparable, IShareGroupData{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2907073346083860543L;
	//96 bit 組合 , 12 world  |  10 process | 10 unit type | 32 time | 32 random , 用3個int 組成
	public int firstId = 0;
	public int secId   = 0;
	public int lastId  = 0;
	/**
	 * server內部暫存的toString的value, 避免要一直toString
	 */
	private transient String cacheString;
	public final static char[] HEX = "0123456789abcdef".toCharArray();
	
	public GUID(){}
	public GUID(int firstId , int secId , int lastId){
		this.firstId = firstId;
		this.secId   = secId;
		this.lastId  = lastId;
	}
	@Override
	public boolean equals(Object arg0) {
		if(arg0 == null) return false;
		GUID campare = (GUID)arg0;
		if(this.firstId == campare.firstId && this.secId == campare.secId && this.lastId == campare.lastId)
			return true;
		return false;
	}
	
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return this.firstId ^ this.secId ^ this.lastId;
	}
	@Override
	public String toString(){
		if(cacheString != null)
			return cacheString;
		
		StringBuilder sb = new StringBuilder();
		toFullHexString(sb,firstId);
		toFullHexString(sb,secId);
		toFullHexString(sb,lastId);
		cacheString =  sb.toString();
		return cacheString;
	}
	/**
	 * 取得guid Type
	 * @return
	 */
	public int getType(){
		return this.firstId & 0x000003ff;
	}
	public static String toString(GUID value){
		if(value == null)
			return null;
		else
			return value.toString();
	}
	public static GUID parseUUID(String value){
		if(value == null || value.length() < 24)
			return  null;
		GUID id = new GUID();
		id.firstId = HexStringToInt(value, 0);
		id.secId   =  HexStringToInt(value,8);
		id.lastId  =  HexStringToInt(value, 16);
		id.cacheString = value;
		return id;
	}
	public static boolean equals(GUID g1, GUID g2){
		if(g1 == null || g2 == null)
			return false;
		return g1.equals(g2);
		
	}
	private static void toFullHexString(StringBuilder sb, int num) {
        char[] chs = new char[Integer.SIZE / 4];
        for(int i = 0; i < chs.length; i++) {
            chs[chs.length - 1 - i] = HEX[(num >> (i * 4)) & 0xf];
        }
        sb.append(chs);
        //return new String(chs);
    }
	private static int HexStringToInt(String value , int offect) {
		int rtn = 0;
		int temp;
        for(int i = 0; i < 8; i++) {
        	temp = Character.getNumericValue(value.charAt(offect + i));
        	rtn |= temp << (28 - i*4);
           // chs[chs.length - 1 - i] = HEX[(num >> (i * 4)) & 0xf];
        }
        return rtn;
    }
	@Override
	public int compareTo(Object o) {
		GUID comapre = (GUID)o;
		if(this.equals(comapre))
			return 0;
		else{
			int result = intCompare(this.firstId,comapre.firstId);
			if(result == 0){
				result = intCompare(this.secId,comapre.secId);
				if(result == 0)
					result = intCompare(this.lastId,comapre.lastId);
			}
			return result;
		}
	}
	private int intCompare(int src, int des){
		if(src == des) return 0;
		else if(src > des) return 1;
		else return -1;
	}
	
	////以下為相容舊版本code產生方式, 以後會全移到GUIDFactory去產生 guid, 以下都會刪除掉
	/**
	 * 随机产生器
	 */
	private static final Random random = new Random();

	/**
	 * 唯一ID计数器
	 */
	private static int uniqueIDCount = 0;


	/**
	 * 生成一个近似全局唯一的ID
	 * 
	 * @return
	 */
	public static String genUniqueID() {
		String lRetID = "";
		long lCurTime = System.currentTimeMillis();
		lRetID += lCurTime & 0xffffff;
		for (int i = 0; i < 6; i++) {
			lRetID += genLetterNumber();
		}
		lRetID += uniqueIDCount;
		uniqueIDCount++;
		return lRetID;
	}

	/**
	 * 生成一个近似全局唯一的ID
	 * 
	 * @param type 类型
	 * @return
	 *
	 * @author lipeilin
	 * @2010-8-20下午06:01:39
	 */
	public static String genUniqueID(String type) {
		return type + "_" + genUniqueID();
	}

	/**
	 * 随机生成一个字母（大小写）或数字
	 * 
	 * @return
	 */
	public static String genLetterNumber() {
		int lRandomNumber = genNextRandomInteger(0, 62);
		if (lRandomNumber >= 0 && lRandomNumber <= 9) {
			return String.valueOf((char) (lRandomNumber + 48));
		} else if (lRandomNumber >= 10 && lRandomNumber <= 35) {
			return String.valueOf((char) (lRandomNumber - 10 + 65));
		} else {
			return String.valueOf((char) (lRandomNumber - 36 + 97));
		}
	}

	/**
	 * 获取一个随机的整数 随机出的数i满足：pMinNumber <= i < pMaxNumber
	 * 
	 * @param min
	 *            随机范围的下界
	 * @param max
	 *            随机范围的上界
	 * @return
	 */
	private static int genNextRandomInteger(int min, int max) {
		if (max - min <= 0) {
			max = min + 1;
		}
		return random.nextInt(max - min) + min;
	}
	
	/**
	 * 生成装备GUID
	 * @return
	 */
	public static String equipmentGuid() {
		return "EQUIP_" + genUniqueID();
	}
	
	/**
	 * 生成物品GUID
	 * @return
	 */
	public static String goodsGuid() {
		return "GOODS_" + genUniqueID();
	}

	/**
	 * 宠物
	 * @return
	 */
	public static String petGuid() {
		return "pet_" + genUniqueID();
	}
	
	/**
	 * 玩家角色
	 * @return
	 */
	public static String playerGuid() {
		return "player_" + genUniqueID();
	}
	
	/**
	 * 公會 
	 * @return
	 */
	public static String guild() {
		return "guild_" + genUniqueID();
	}

	/**
	 * 信件
	 * @return
	 */
	public static String mailGuid() {
		return "mail_" + genUniqueID();
	}
	/**
	 * 屍體
	 * @return
	 */
	public static String corpseGuid() {
		return "corpse_" + genUniqueID();
	}
	
	/**
	 * 
	 * @return
	 */
	public int getWorldId(){
		return (this.firstId >> 20);
	}
	
}
